package com.jas.common;
//@author A0098803W
import java.util.Comparator;

//@author A0098803W
/**
 * This class handles the date comparison for events stored in the file. It
 * overrides the <code>compare()</code> method of the <code>Comparator</code>
 * class.
 * 
 * 
 * 
 */
class EventDateComparator implements Comparator<Event> {
	private String sortOrder;

	//@author A0098803W
	/**
	 * This constructor specifies the type of sorting for the events, whether it
	 * is <code>asc</code> for ascending or <code>desc</code> for descending
	 * order.
	 * 
	 * @param sortOrder
	 *            <code>asc</code> for ascending or <code>desc</code> for
	 *            descending order
	 */
	EventDateComparator(String sortOrder) {
		this.sortOrder = sortOrder.toLowerCase();
	}

	//@author A0098803W
	@Override
	public int compare(Event event1, Event event2) {

		if (event1 instanceof TimedEvent && event2 instanceof TimedEvent) {
			TimedEvent timedEvent1 = (TimedEvent) event1;
			TimedEvent timedEvent2 = (TimedEvent) event2;

			if (this.sortOrder.equalsIgnoreCase("asc")) {
				return timedEvent1.getEventEndDateTime().compareTo(
						timedEvent2.getEventEndDateTime());
			} else {
				return timedEvent2.getEventEndDateTime().compareTo(
						timedEvent1.getEventEndDateTime());

			}

		} else if (event1 instanceof DeadlineEvent
				&& event2 instanceof DeadlineEvent) {

			DeadlineEvent deadlineEvent1 = (DeadlineEvent) event1;
			DeadlineEvent deadlineEvent2 = (DeadlineEvent) event2;
			if (this.sortOrder.equalsIgnoreCase("asc")) {
				return deadlineEvent1.getEventEndDateTime().compareTo(
						deadlineEvent2.getEventEndDateTime());
			} else {
				return deadlineEvent2.getEventEndDateTime().compareTo(
						deadlineEvent1.getEventEndDateTime());
			}

		}
		if (this.sortOrder.equalsIgnoreCase("asc")) {
			return event1.getEventDescription().compareTo(
					event2.getEventDescription());
		}
		return event2.getEventDescription().compareTo(
				event1.getEventDescription());

	}
}